<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>K 次操作后最大化顶端元素</title>
</head>
<body>
  <script>
    function fn(nums, k) {
      if (k === 0) {
        return nums[0]
      }
      if (k === 1) {
        return nums[1] ?? -1
      }
      if (nums.length === 1 && (k % 2 === 1)) {
        return -1
      }
      let ans = nums.slice(0, k - 1)
      return Math.max(...ans, nums[k] || 0)
    }
    console.log(fn([5, 2, 2, 4, 0, 6], 4))
  </script>
</body>
</html>